$(function () {

    for (area in Addr){
        $("#country").append("<option value='"+area+"'>"+area+"</option>");
    }
    layui.form.render();


    /*验证手机号格式*/
    $("#telephone").blur(function () {
        var name = $("#telephone").val();
        if (isNaN(name)){
            layer.msg('请您输入正确的手机号！');
            $("#telephone").val("");
        }
    })

    $("#adminTelephone").blur(function () {
        var tele = $("#adminTelephone").val();
        if (isNaN(tele)){
            layer.msg('请您输入正确的手机号！');
            $("#adminTelephone").val("");
        }
    })

    /*验证密码格式*/
     $("#password").blur(function () {
         var password = $("#password").val();
         if (password.length < 6){
             layer.msg('密码小于六位请重新输入！');
             $("#password").val("");
         }
     })

    /*判断密码是否一致*/
    $("#againPassword").blur(function () {
        var password1 = $("#password").val();
        var password = $("#againPassword").val();
        if (password != password1){
            layer.msg('密码不一致请重新输入！');
            $("#againPassword").val("");
        }
    })

    /*发送验证码*/
    $(".shiku-code").click(function () {
        if ($("#telephone").val() === ""){
            layer.msg("请输入手机号码！");
            return;
        }
        var data={
            telephone:$("#telephone").val()
        }
        $.ajax({
            type:"POST",
            url:"/mp/sendCode",
            dataType:"json",
            data:data,
            contentType : 'application/x-www-form-urlencoded; charset=UTF-8',
            async:false,
            traditional: true,
            success : function(result) {
                console.log(result)
               if (result.resultCode != 1){
                   layer.msg(result.resultMsg);
               }else {
                   layer.msg("发送成功！");
               }
            },
            error : function(result) {
                layer.msg(result.resultMsg);
            }
        })
    })

    $("#companyBusinessLicense").blur(function () {
        var cbl = $("#companyBusinessLicense").val();
        if (cbl.length === 18 || cbl.length === 15 ){
        }else{
            layer.msg("请输入正确的营业执照号！");
            $("#companyBusinessLicense").val("");
        }
    })

    $("#desc").blur(function () {
        var desc = $("#desc").val();
        if (desc == '' && desc.length == 0){
            layer.msg("详细地址信息不能为空！");
        }
    })

    $("#adminID").blur(function () {
        var id = $("#adminID").val();
        if (id.length > 18 || id.length < 18){
            layer.msg("请输入正确的身份证号码！")
            $("#adminID").val("")
        }
    })
})

let country='';
layui.form.on('select(country)',function(data){
    console.log(data.value);
    country=data.value;
    for(s in Addr[data.value]){
        $("#province").append("<option value='"+s+"'>"+s+"</option>");
    }
    layui.form.render();
})

layui.form.on('select(province)',function(data){
    for(c in Addr[country][data.value]){
        $("#city").append("<option value='"+Addr[country][data.value][c]+"'>"+Addr[country][data.value][c]+"</option>");
    }
    layui.form.render();
})

/*提交表单*/
layui.form.on('submit(formDemo)', function (data) {
    //JSON.stringify(data.field)   这是表单中所有的数据
    var data = {
        telephone:$("#telephone").val(),
        password:$("#password").val(),
        companyName:$("#companyName").val(),
        companyBusinessLicense:$("#companyBusinessLicense").val(),
        companyType:$(".companyType").val(),
        adminName:$("#adminName").val(),
        adminID:$("#adminID").val(),
        adminTelephone:$("#adminTelephone").val(),
        country:$("#country").val(),
        province:$("#province").val(),
        city:$("#city").val(),
        desc:$("#desc").val(),
        randcode:$("#randcode").val()
    }

    console.log(data)
    $.ajax({
        type:"POST",
        url:"/mp/opffcialInfoRegister",
        dataType:"json",
        data:data,
        async:false,
        success : function(result) {
            console.log(result)
           if (result.resultCode == 1){
               location.href="/mp/success.html";
           }else if(result.resultCode == 1040104){
               layer.msg("验证码过期或失效！");
           }
           layer.msg(result.resultMsg);
           return false;

        },
        error : function(result) {
            layer.msg(result.resultMsg);
        }
    })
})

